package cs131.pa1.filter.sequential;

import cs131.pa1.filter.Message;

import java.util.LinkedList;
import java.util.List;

public class GrepCommand extends SequentialFilter {
	public final static String COMMAND = "grep";

	private String para;

	public GrepCommand(List<String> commands) {

		if(commands.size() <= 1){
			System.out.print(Message.REQUIRES_PARAMETER.with_parameter(COMMAND));
			setInterruption(true);
			return;
		}

		para = commands.get(1);
	}

	public void process(){

		if(isInterruption()){
			return;
		}

		if(input == null){
			System.out.print(Message.REQUIRES_INPUT.with_parameter(COMMAND + " " + para));
			return;
		}
		super.process();

		if (next == null) {
			for (String outInfo : output) {
				System.out.println(outInfo);
			}
		}
	}

	public String processLine(String Line){

		if(output == null){
			this.output = new LinkedList<String>();
		}

		if(Line.indexOf(para) != -1){
			return Line;
		}
		return null;
	}
	
}
